#ifndef CAN_CTRL_H
#define CAN_CTRL_H

#include <stdint.h>
#include <pthread.h>
#include <iostream>

#include "controlcan.h"

#define LEN_MAX 8
#define USBCAN_MAX 50

/** @defgroup CAN_identifier_type CAN Identifier Type
  * @{
  */
#define CAN_ID_STD                  (0x00000000U)  /*!< Standard Id */
#define CAN_ID_EXT                  (0x00000004U)  /*!< Extended Id */
/**
  * @}
  */

/** @defgroup CAN_remote_transmission_request CAN Remote Transmission Request
  * @{
  */
#define CAN_RTR_DATA                (0x00000000U)  /*!< Data frame   */
#define CAN_RTR_REMOTE              (0x00000002U)  /*!< Remote frame */

/**
  * @brief  CAN Rx message header structure definition
  */
typedef struct
{
    uint32_t StdId;    /*!< Specifies the standard identifier.
                            This parameter must be a number between Min_Data = 0 and Max_Data = 0x7FF. */

    uint32_t ExtId;    /*!< Specifies the extended identifier.
                            This parameter must be a number between Min_Data = 0 and Max_Data = 0x1FFFFFFF. */

    uint32_t IDE;      /*!< Specifies the type of identifier for the message that will be transmitted.
                            This parameter can be a value of @ref CAN_identifier_type */

    uint32_t RTR;      /*!< Specifies the type of frame for the message that will be transmitted.
                            This parameter can be a value of @ref CAN_remote_transmission_request */

    uint32_t DLC;      /*!< Specifies the length of the frame that will be transmitted.
                            This parameter must be a number between Min_Data = 0 and Max_Data = 8. */

    uint32_t Timestamp; /*!< Specifies the timestamp counter value captured on start of frame reception.
                            @note: Time Triggered Communication Mode must be enabled.
                            This parameter must be a number between Min_Data = 0 and Max_Data = 0xFFFF. */

    uint32_t FilterMatchIndex; /*!< Specifies the index of matching acceptance filter element.
                            This parameter must be a number between Min_Data = 0 and Max_Data = 0xFF. */

    uint8_t Data[8];


} CAN_RxHeaderTypeDef;


class Can_Ctrl {
public:
    Can_Ctrl(){std::cout<<"Can_Ctrl"<<std::endl;}
    ~Can_Ctrl(){std::cout<<"~Can_Ctrl"<<std::endl;}
    int can_ctrl_init();
    int can_ctrl_deinit();
    inline int is_ready() {return this->ready;}
    inline uint8_t can_thread_getflag(){return run_flag;}
    void can_thread_enable(){this->run_flag = 1;}
    void can_thread_disable(){this->run_flag = 0;}
    int send_func(CAN_RxHeaderTypeDef* pHeader);          
protected:

private:
    uint8_t data[LEN_MAX];  //数据位
    uint8_t len;            //数据位长度
    uint8_t ready = 0;

    VCI_BOARD_INFO pInfo[USBCAN_MAX];   //用来获取设备信息
    VCI_CAN_OBJ recv[3000];             //接收缓存，设为3000为佳
    VCI_CAN_OBJ send[1];                //待发送帧   

    uint8_t run_flag = 0;               //线程运行标志
    pthread_t pid;      

    friend void *can_receive_thread(void* param); 
};

#endif